//
//  XWXWaterfallFlowLayout.m
//  XWXWaterfallFlow
//
//  Created by 许卫星 on 16/12/14.
//  Copyright © 2016年 许卫星. All rights reserved.
//

#import "XWXWaterfallFlowLayout.h"
/**默认列数**/
static const NSInteger kXWXDefaultColumnCount = 3;
/**默认水平间距**/
static const NSInteger kXWXDefaultColumnMargin = 10;
/**默认垂直间距**/
static const NSInteger kXWXDefauleRowMargin = 10;
/**默认边框间隔**/
static const UIEdgeInsets kXWXDefaultEdgeInsets = {10,10,10,10};

@interface XWXWaterfallFlowLayout()
/**每列高度数据**/
@property (nonatomic, strong) NSMutableArray *columsHighs;
/**每个Item的Attributes**/
@property (nonatomic, strong) NSMutableArray *itemAttributes;
/**collectionView的Content高度**/
@property (nonatomic, assign) CGFloat collectionContentH;
@end

@implementation XWXWaterfallFlowLayout

/**
 *重新布局
 */
- (void)prepareLayout
{
    [super prepareLayout];
    //1.清空原始数据
    self.collectionContentH = 0;
    [self.columsHighs removeAllObjects];
    [self.itemAttributes removeAllObjects];
    
    //2.初始化新数据
    //初始化每列高度
    for (int i = 0; i < self.columnCount; i ++) {
        [self.columsHighs addObject:@(self.edgeInsets.top)];
    }
    
    //初始化itemAttributes
    NSInteger itemCount = [self.collectionView numberOfItemsInSection:0];
    for (int i = 0; i < itemCount; i ++) {
        //获取Item的位置
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        //根据位置计算item的frame
        UICollectionViewLayoutAttributes *layoutAttributes = [self collectionViewLayoutAttributesWithIndexPatch:indexPath];
        [self.itemAttributes addObject:layoutAttributes];
    }
}


/**
 根据位置计算item的frame
 @param indexPatch item的位置
 @return item的frame
 */
- (UICollectionViewLayoutAttributes *)collectionViewLayoutAttributesWithIndexPatch:(NSIndexPath *)indexPatch
{
    UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPatch];
    //计算frame
    CGFloat collectionViewW = self.collectionView.frame.size.width;
    
    CGFloat w = (collectionViewW - self.edgeInsets.left - self.edgeInsets.right - (self.columnMargin * self.columnCount - 1)) / self.columnCount;
    CGFloat h = [self.delegate waterfallFlowLayout:self itemIndexPatch:indexPatch itemW:w];
    
    //最短列所在位置
    NSInteger shortColumnIndex = 0;
    //获取最短列
    CGFloat shortColumn = [self.columsHighs[0] floatValue];
    for (int i = 1; i < self.columsHighs.count; i ++) {
        CGFloat columsHigh = [self.columsHighs[i] floatValue];
        if (shortColumn > columsHigh) {
            shortColumn = columsHigh;
            shortColumnIndex = i;
        }
    }
    CGFloat x = self.edgeInsets.left + shortColumnIndex * (w + self.columnMargin);
    //获取最短列的y
    CGFloat y = [self.columsHighs[shortColumnIndex] floatValue];
    if (y != self.edgeInsets.top) {
        y += self.rowMargin;
    }
    
    layoutAttributes.frame = CGRectMake(x, y, w, h);
    
    //更新最短列的数据
    self.columsHighs[shortColumnIndex] = @(CGRectGetMaxY(layoutAttributes.frame));
    
    //获取内容高度
    CGFloat columnHigh = [self.columsHighs[shortColumnIndex] floatValue];
    if (self.collectionContentH < columnHigh) {
        self.collectionContentH = columnHigh;
    }
    return layoutAttributes;
}


/**
 Item的布局属性
 @param rect Item的rect
 @return Item的LayoutAttributes
 */
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
{
    return self.itemAttributes;
}


/**
 collectionView 内容大小

 @return collectionView的ContentSize
 */
- (CGSize)collectionViewContentSize
{
    return CGSizeMake(0, self.collectionContentH + kXWXDefaultEdgeInsets.bottom);
}

#pragma mark - lazy
- (NSMutableArray *)columsHighs
{
    if (_columsHighs == nil) {
        _columsHighs = [[NSMutableArray alloc] init];
    }
    return _columsHighs;
}

- (NSMutableArray *)itemAttributes
{
    if (_itemAttributes == nil) {
        _itemAttributes = [[NSMutableArray alloc] init];
    }
    return _itemAttributes;
}

- (CGFloat)rowMargin
{
    if ([self.delegate respondsToSelector:@selector(rowMarginInWaterflowLayout:)]) {
        return [self.delegate rowMarginInWaterflowLayout:self];
    } else {
        return kXWXDefauleRowMargin;
    }
}

- (CGFloat)columnMargin
{
    if ([self.delegate respondsToSelector:@selector(columnMarginInWaterflowLayout:)]) {
        return [self.delegate columnMarginInWaterflowLayout:self];
    } else {
        return kXWXDefaultColumnMargin;
    }
}

- (NSInteger)columnCount
{
    if ([self.delegate respondsToSelector:@selector(columnCountInWaterflowLayout:)]) {
        return [self.delegate columnCountInWaterflowLayout:self];
    } else {
        return kXWXDefaultColumnCount;
    }
}

- (UIEdgeInsets)edgeInsets
{
    if ([self.delegate respondsToSelector:@selector(edgeInsetsInWaterflowLayout:)]) {
        return [self.delegate edgeInsetsInWaterflowLayout:self];
    } else {
        return kXWXDefaultEdgeInsets;
    }
}
@end
